#include <iostream>
#include <string>
//
// Created by Asus on 2025/6/1.
//
using namespace std;

class Person {
public:
    int m_A;
    int m_B;

    Person() {

    }

    Person(int a, int b) {
        this->m_A = a;
        this->m_B = b;
    }

    //成员函数实现 + 号运算符重载
    Person operator+(const Person &other) {
        cout << "operator+(const Person &other)" << endl;
        Person temp;
        temp.m_A = this->m_A + other.m_A;
        temp.m_B = this->m_B + other.m_B;
        return temp;
    }
};

//全局函数实现 + 号运算符重载
// Person operator+(const Person& p1, const Person& p2) {
//     cout << "operator+(const Person& p1, const Person& p2)" << endl;
//     Person temp(0, 0);
//     temp.m_A = p1.m_A + p2.m_A;
//     temp.m_B = p1.m_B + p2.m_B;
// }

//运算符重载 可以发生函数重载
Person operator+(const Person &p2, int val) {
    cout << "operator+(const Person &p2, int val)" << endl;
    Person temp;
    temp.m_A = p2.m_A + val;
    temp.m_B = p2.m_B + val;
    return temp;
}

void demo01() {
    Person p1(10, 10);
    Person p2(20, 20);

    //成员函数方式
    Person p3 = p2 + p1; //相当于 p2.operaor+(p1)
    cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;


    Person p4 = p3 + 10; //相当于 operator+(p3,10)
    cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;

    // Person p5 = operator+(p1, p2);  // 显式调用全局的operator+函数
    // cout << "mA:" << p5.m_A << " mB:" << p5.m_B << endl;
}

class Person2 {
    friend ostream& operator<<(ostream& out, Person2& p);
private:
    int m_A;
    int m_B;
public:
    Person2(int a, int b) {
        this->m_A = a;
        this->m_B = b;
    }

    //成员函数 实现不了  p << cout 不是我们想要的效果
    //void operator<<(Person& p){
    //}
};

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person2& p) {
    out << "a:" << p.m_A << " b:" << p.m_B;
    return out;
}

void demo02() {
    Person2 p1(10, 10);
    cout << p1 << " hello world" << endl;
}

int main(int argc, char *argv[]) {
    // demo01();
    demo02();
    return 0;
}
